// pages/volunteer/apply/index.js
import Notify from '../../../miniprogram_npm/@vant/weapp/notify/notify';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    phone:'',
    address:'',
    idcard:'', 
    status:0,
    organizations:[],
    otitle:'',
    otitle_show:'',//显示单位文字
    oid:-1,
    show:false,
    manual_otitle:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getOrganizations();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onShareTimeline(){

  },
  fail(msg){
    return {
      'error':true,
      'msg': msg
    }
  },
  success() {
    return {
      'error':false,
      'msg': ''
    }
  },//表单验证
  validateFormData(){
    const that = this

    if(that.data.oid<0) {
      return that.fail('请选择所在单位')
    }

    if(that.data.oid == 0) {
      if(!that.data.manual_otitle) {
        return that.fail('请手动录入所在单位')
      }
      else {
        that.data.otitle = that.data.manual_otitle;
      }
    }


    if(!that.data.name) {
      return that.fail('姓名不能为空')
    }
    if(!that.data.phone) {
      return that.fail('手机号不能为空')
    }
    // if(!that.data.address) {
    //   return that.fail('地址不能为空')
    // }
    if(!that.data.idcard) {
      return that.fail('身份证不能为空')
    }


    return that.success();
  },//提交表单
  submitApply() {
    //提交审核
    const that = this
    let validate = that.validateFormData();
    if(validate.error) {
      that.notifyFail(validate.msg);
      return ;
    }
    const volunteer = {
      name:that.data.name,
      phone:that.data.phone,
      // address:that.data.address,
      idcard:that.data.idcard,
      oid:that.data.oid,
      otitle:that.data.otitle
    }

    app.api.post(app.getUrl('volunteer.apply'),volunteer).then((res)=>{
      console.log(res)  
      that.notifySuccess();
    }).catch(err=>{
      console.log(err)
      that.notifyFail(err.msg)
    })
  },
  notifySuccess() {
    Notify({ 
      type: 'primary', 
      message: '已提交审核,请耐心等待',
      // safeAreaInsetTop:true,
      onClose:()=>{
        wx.navigateBack({
          delta: 0,
        })
      }
     });
  },
  notifyFail(msg='提交失败', navBack=false) {
    Notify({ 
      type: 'danger', 
      message: msg,
      onClose:()=>{
        if(navBack) {
          wx.navigateBack({
            delta: 0,
          })
        }
      }
     });
  },
  getOrganizations() {
    const that = this
    app.api.get(app.getUrl('volunteer.organizations')).then((res)=>{
      that.data.organizations = res;
      that.data.organizations.push({
        id:0,
        title:'其他',
      })
      that.setData({
        organizations:that.data.organizations
      })
    })
  },
  openPop() {
    const that = this
    that.setData({
      show:true
    })
  },
  onClose() {
    const that = this
    that.setData({
      show:false
    })
  },
  cancelOrg() {
    const that = this
    that.setData({
      show:false
    })
  },
  selectOrg(e) {
    console.log(e)
    const that = this
    that.setData({
      oid:e.detail.value.id,
      otitle_show:e.detail.value.title,
      otitle:e.detail.value.title,
      show:false
    })
  }


})